package com.vercon.commonmirror.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;


/**
 * Created by csh on 2020/8/29.
 * 判断是否有网络，并监听WiFi信号强度
 */
public class NetBroadcastReceiver extends BroadcastReceiver {

    private NetConnectedListener netConnectedListener;
    private int value;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (value > 1) {
            value = 0;
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
            try {
                if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected() && !isNetWorkAvailable(context)) {
                    //WIFI和移动网络均未连接
                    netConnectedListener.isWifiContent(false);
                }
            } catch (Exception e) {

            }
            try {
                if (wifiNetInfo.isConnected() && isNetWorkAvailable(context)) {
                    //WIFI连接
                    netConnectedListener.isWifiContent(true);
                }
            } catch (Exception e) {

            }

            //以太网连接
            netConnectedListener.isEthernetContent(networkInfo.isConnected() && isNetWorkAvailable(context));
//            LogUtils.i("NetBroadcastReceiver", isNetWorkAvailable(context) + "  " +
//                    mobNetInfo.isConnected() + "  " + wifiNetInfo.isConnected() + "  " + networkInfo.isConnected());
        } else {
            value += 1;
        }

//        getWifiInfo(context);
    }

    private void getWifiInfo(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (wifiInfo.getBSSID() != null) {
            //wifi名称
            String ssid = wifiInfo.getSSID();
            //wifi信号强度
            int signalLevel = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 5);
            //wifi速度
            int speed = wifiInfo.getLinkSpeed();
            //wifi速度单位
            String units = WifiInfo.LINK_SPEED_UNITS;
            Log.e("WifiReceiver", "网络状态改变" + "ssid=" + ssid + ",signalLevel=" + signalLevel + ",speed=" + speed + ",units=" + units);
        }
    }

    public void setNetConnectedListener(NetConnectedListener netConnectedListener) {
        this.netConnectedListener = netConnectedListener;
    }

    public interface NetConnectedListener {
        void isWifiContent(boolean isConnected);

        void isEthernetContent(boolean isConnected);
    }

    /**
     * @return 是否有网络
     */
    public static boolean isNetWorkAvailable(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager == null) {
            return false;
        }
        NetworkInfo[] networkInfos = manager.getAllNetworkInfo();
        if (networkInfos != null) {
            for (NetworkInfo info : networkInfos) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        return false;
    }
}
